<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务类型" prop="taskType">
        <el-select v-model="queryParams.taskType" placeholder="请选择任务类型" clearable>
          <el-option
            v-for="dict in zsws_task_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeTaskTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="任务状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择任务状态" clearable>
          <el-option
            v-for="dict in task_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="网点id" prop="bankId">
        <el-input
          v-model="queryParams.bankId"
          placeholder="请输入网点id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="送箱类型" prop="boxTypeList">
        <el-input
          v-model="queryParams.boxTypeList"
          placeholder="请输入送箱类型"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="吉箱/实箱" prop="emptyOrFullBox">
        <el-input
          v-model="queryParams.emptyOrFullBox"
          placeholder="请输入吉箱/实箱"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所送款箱所属网点" prop="boxOwnerId">
        <el-input
          v-model="queryParams.boxOwnerId"
          placeholder="请输入所送款箱所属网点"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['service:zswsTask:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['service:zswsTask:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['service:zswsTask:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['service:zswsTask:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="zswsTaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID，自增" align="center" prop="id" />
      <el-table-column label="任务类型" align="center" prop="taskType">
        <template #default="scope">
          <dict-tag :options="zsws_task_type" :value="scope.row.taskType"/>
        </template>
      </el-table-column>
      <el-table-column label="任务时间" align="center" prop="taskTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.taskTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="task_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="取消类型" align="center" prop="cancelType" />
      <el-table-column label="取消原因" align="center" prop="cancelReason" />
      <el-table-column label="网点id" align="center" prop="bankId" />
      <el-table-column label="送箱类型" align="center" prop="boxTypeList" />
      <el-table-column label="箱号清单" align="center" prop="boxList" />
      <el-table-column label="吉箱/实箱" align="center" prop="emptyOrFullBox" />
      <el-table-column label="所送款箱所属网点" align="center" prop="boxOwnerId" />
      <el-table-column label="新袋个数" align="center" prop="blackBoxNew" />
      <el-table-column label="旧袋个数" align="center" prop="blackBoxOld" />
      <el-table-column label="其他袋个数" align="center" prop="blackBoxOther" />
      <el-table-column label="任务备注描述" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['service:zswsTask:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['service:zswsTask:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改网点任务对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="zswsTaskRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务类型" prop="taskType">
          <el-select v-model="form.taskType" placeholder="请选择任务类型">
            <el-option
              v-for="dict in zsws_task_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任务时间" prop="taskTime">
          <el-date-picker clearable
            v-model="form.taskTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择任务时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择任务状态">
            <el-option
              v-for="dict in task_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="取消原因" prop="cancelReason">
          <el-input v-model="form.cancelReason" placeholder="请输入取消原因" />
        </el-form-item>
        <el-form-item label="网点id" prop="bankId">
          <el-input v-model="form.bankId" placeholder="请输入网点id" />
        </el-form-item>
        <el-form-item label="送箱类型" prop="boxTypeList">
          <el-input v-model="form.boxTypeList" placeholder="请输入送箱类型" />
        </el-form-item>
        <el-form-item label="箱号清单" prop="boxList">
          <el-input v-model="form.boxList" placeholder="请输入箱号清单" />
        </el-form-item>
        <el-form-item label="吉箱/实箱" prop="emptyOrFullBox">
          <el-input v-model="form.emptyOrFullBox" placeholder="请输入吉箱/实箱" />
        </el-form-item>
        <el-form-item label="所送款箱所属网点" prop="boxOwnerId">
          <el-input v-model="form.boxOwnerId" placeholder="请输入所送款箱所属网点" />
        </el-form-item>
        <el-form-item label="新袋个数" prop="blackBoxNew">
          <el-input v-model="form.blackBoxNew" placeholder="请输入新袋个数" />
        </el-form-item>
        <el-form-item label="旧袋个数" prop="blackBoxOld">
          <el-input v-model="form.blackBoxOld" placeholder="请输入旧袋个数" />
        </el-form-item>
        <el-form-item label="其他袋个数" prop="blackBoxOther">
          <el-input v-model="form.blackBoxOther" placeholder="请输入其他袋个数" />
        </el-form-item>
        <el-form-item label="任务备注描述" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ZswsTask">
import { listZswsTask, getZswsTask, delZswsTask, addZswsTask, updateZswsTask } from "@/api/service/zswsTask"

const { proxy } = getCurrentInstance()
const { task_status, zsws_task_type } = proxy.useDict('task_status', 'zsws_task_type')

const zswsTaskList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const daterangeTaskTime = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    taskType: null,
    taskTime: null,
    status: null,
    cancelType: null,
    bankId: null,
    boxTypeList: null,
    emptyOrFullBox: null,
    boxOwnerId: null,
  },
  rules: {
    taskType: [
      { required: true, message: "任务类型不能为空", trigger: "change" }
    ],
    taskTime: [
      { required: true, message: "任务时间不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "任务状态不能为空", trigger: "change" }
    ],
    bankId: [
      { required: true, message: "网点id不能为空", trigger: "blur" }
    ],
    boxTypeList: [
      { required: true, message: "送箱类型不能为空", trigger: "blur" }
    ],
    boxList: [
      { required: true, message: "箱号清单不能为空", trigger: "blur" }
    ],
    emptyOrFullBox: [
      { required: true, message: "吉箱/实箱不能为空", trigger: "blur" }
    ],
    boxOwnerId: [
      { required: true, message: "所送款箱所属网点不能为空", trigger: "blur" }
    ],
    blackBoxNew: [
      { required: true, message: "新袋个数不能为空", trigger: "blur" }
    ],
    blackBoxOld: [
      { required: true, message: "旧袋个数不能为空", trigger: "blur" }
    ],
    blackBoxOther: [
      { required: true, message: "其他袋个数不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询网点任务列表 */
function getList() {
  loading.value = true
  queryParams.value.params = {}
  if (null != daterangeTaskTime && '' != daterangeTaskTime) {
    queryParams.value.params["beginTaskTime"] = daterangeTaskTime.value[0]
    queryParams.value.params["endTaskTime"] = daterangeTaskTime.value[1]
  }
  listZswsTask(queryParams.value).then(response => {
    zswsTaskList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    taskType: null,
    taskTime: null,
    status: null,
    cancelType: null,
    cancelReason: null,
    bankId: null,
    boxTypeList: null,
    boxList: null,
    emptyOrFullBox: null,
    boxOwnerId: null,
    blackBoxNew: null,
    blackBoxOld: null,
    blackBoxOther: null,
    remark: null,
    createTime: null,
    createBy: null,
    deliverTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm("zswsTaskRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeTaskTime.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加网点任务"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getZswsTask(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改网点任务"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["zswsTaskRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateZswsTask(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addZswsTask(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除网点任务编号为"' + _ids + '"的数据项？').then(function() {
    return delZswsTask(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('service/zswsTask/export', {
    ...queryParams.value
  }, `zswsTask_${new Date().getTime()}.xlsx`)
}

getList()
</script>
